package com.oig.sys.auth.controller;


import com.oig.common.exception.BadRequestException;
import com.oig.sys.auth.service.IUserSyncService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "用户同步,同步飞书部门，员工")
@RestController
@RequestMapping("/userSync")
@Slf4j
@RequiredArgsConstructor
public class UserSyncController {

    private final IUserSyncService userSyncService;


    @Value("${sys.feishu.app-id}")
    private String appId;
    @Value("${sys.feishu.app-secret}")
    private String appSecret;

    @Operation(summary = "同步飞书组织结构和用户")
    @PostMapping(value = "/syncDepartAndUserFromFeishu")
    public void syncDepartAndUserFromFeishu(@RequestParam("tenantId") Integer tenantId) throws Exception {
        log.info("sync users info from Feishu， tenantId={},appId={}, appSecret={}",tenantId, appId, appSecret);
        if (tenantId==0){
            throw new BadRequestException("请选正确的租户");
        }
        userSyncService.syncDepartFromFeishu(appId, appSecret, tenantId);
    }

    @Operation(summary = "同步飞书用户")
    @PostMapping(value = "/syncUserFromFeishu")
    public void syncUserFromFeishu(@RequestParam("tenantId") Integer tenantId) throws Exception {
        log.info("sync users info from Feishu， tenantId={}, appId={}, appSecret={}",tenantId, appId, appSecret);
        if (tenantId==0){
            throw new BadRequestException("请选正确的租户");
        }
        userSyncService.syncUserFromFeishu(appId, appSecret, tenantId);
    }

}
